/*
 * Created on Mar 3, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package littlecyberwar.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;

import littlecyberwar.ModelChangedListener;
import littlecyberwar.model.*;
import littlecyberwar.tool.*;
import littlecyberwar.ui.*;
import littlecyberwar.util.*;

import ModuleBuilder.model.*;

/**
 * 
 * @author Milton  Soong
 *
 *	The Scaling information is ONLY used in outputing to screen using 
 *	g2. All otrher measurement is using original unscaled information.
 */
public class MapDrawerDelegate {

	Image[][] mapImageArray;
	int col;
	int row;
	int mapElementWidth;
	int mapElementHeight;
	int horizontalOffsets[];
	int verticalOffsets[];
	double scale = 1.0;		// the scaling info input to indicate how to output

	public void setMapImage( ImageElement[][] allImg, int colIn, int rowIn, boolean useLargeIcon) {
	
		col = colIn;
		row = rowIn;
		mapImageArray = new Image[col][row];
	  	
		// now setup the horizontal and the vertical offsets
		if (useLargeIcon) {
			mapElementWidth = allImg[0][0].getFullImageIcon().getIconWidth();
			mapElementHeight = allImg[0][0].getFullImageIcon().getIconHeight();
		} else {
			mapElementWidth = allImg[0][0].getSmallIcon().getIconWidth();
			mapElementHeight = allImg[0][0].getSmallIcon().getIconHeight();
		}
		horizontalOffsets = new int[col];
		verticalOffsets = new int[row];
	  	
		for (int i=0; i < col; i++ ) {
			for (int j=0; j< row; j++) {
				ImageElement el = allImg[i][j];
				Image img=null;
				if ( useLargeIcon == true ) {
					img = el.getFullImageIcon().getImage();
				} else {
					// Debug.out("setMapImage==>smalllIcon:"+ el.getSmallIcon().toString());
					img = el.getSmallIcon().getImage();
				}				
				mapImageArray[i][j] = img;
				verticalOffsets[j] = j*mapElementHeight;
			}
			horizontalOffsets[i]=i*mapElementWidth;
		}
		return;		
	}
	
	public void drawMap(Graphics2D g2) {

		if ( mapImageArray != null ) {
			for (int i=0; i < col; i++ ) {
				for (int j=0; j< row; j++) {
					g2.drawImage(mapImageArray[i][j], 
								(int)(horizontalOffsets[i]), 
								(int)(verticalOffsets[j]), 
								null);
				}
			}
		}
		return;
	}
	
	public Dimension getMapSize() {
		Dimension d = new Dimension(col*mapElementWidth,row*mapElementHeight);
		Dimension scaledD = ScalingDelegate.getScaledDimension(d);
		return ( scaledD );
	}
	
	public void setScaling( double scaleIn ){
		scale = scaleIn;
	}
}
